<?php 
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class Admin_IndexController extends Zend_Controller_Action
{
	
	public function init(){
		$registry = Zend_Registry::getInstance();
		$this->_db = $registry->get('db');
	}
    public function indexAction()
    {
        $this->_helper->layout->setLayout('adminlogin');
        # If we're already logged in, just redirect
        if(Zend_Auth::getInstance()->hasIdentity())
        {
            $this->_redirect('/admin/index/adminview');
        }
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->messages = $this->_flashMessenger->getMessages();  

        $request = $this->getRequest();
        $loginForm = $this->getLoginForm();

        $errorMessage = "";

        if($request->isPost())
        {
            if($loginForm->isValid($request->getPost()))
            {

                $authAdapter = $this->getAuthAdapter();

                # get the nickname and password from the form
                $nickname = $loginForm->getValue('nickname');
                $password = $loginForm->getValue('password');
				$password = md5($password);

                # pass to the adapter the submitted nickname and password
                $authAdapter->setIdentity($nickname)
                            ->setCredential($password);
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);

                # is the user a valid one?
                if($result->isValid())
                {
                    # all info about this user from the login table
                    # ommit only the password, we don't need that
                    $userInfo = $authAdapter->getResultRowObject(null, 'password');

                    # the default storage is a session with namespace Zend_Auth
                    $authStorage = $auth->getStorage();
                    $authStorage->write($userInfo);
                    
                    

                    $this->_redirect('/admin/index');
                }
                else
                {
                    $errorMessage = "Wrong username or password provided. Please try again.";
                }
            }
        }
        $this->view->messages[] = $errorMessage;
        $this->view->loginForm = $loginForm;
    }

    public function logoutAction()
    {
        # clear everything - session is cleared also!
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('admin/index');
    }

    /**
     * Gets the adapter for authentication against a database table
     *
     * @return object
     */
    protected function getAuthAdapter()
    {
    	//Zend_Loader::loadFile('Zend/Auth/Adapter/DbTable.php');
        //$dbAdapter = Zend_Registry::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($this->_db);

        $authAdapter->setTableName('user')
                    ->setIdentityColumn('nickname')
                    ->setCredentialColumn('password')
                    ->setCredentialTreatment('(?)');
                    
        return $authAdapter;
    }

    /**
     * Create and return the login form
     *
     * @return object
     */
    protected function getLoginForm()
    {
    	//Zend_Loader::loadFile('Zend/Form/Element/Text.php');
    	//Zend_Loader::loadFile('Zend/Form/Element/Password.php');
        //Zend_Loader::loadFile('Zend/Form/Element/Submit.php');
        //Zend_Loader::loadFile('Zend/Form.php');
        $nickname = new Zend_Form_Element_Text('nickname');
        $nickname->setLabel('Nickname:')
                ->setRequired(true);

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
                ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('login');
        $submit->setLabel('Login');

        $loginForm = new Zend_Form();
        $loginForm->setAction($this->_request->getBaseUrl().'/login/index/')
                ->setMethod('post')
                ->addElement($nickname)
                ->addElement($password)
                ->addElement($submit);

        return $loginForm;
    }
    public function forbiddenAction(){
    	
    }
  
  public function adminviewAction()
  {
  	$this->_helper->layout->setLayout('admin');
  	if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $this->_redirect('admin');
    }
  	Zend_Loader::loadFile('../application/admin/models/User.php');   	
  	$user_model = new User();
  	$userInfo = Zend_Auth::getInstance()->getStorage()->read();
	$this->view->nickname = $userInfo->nickname;
	$this->view->name = $userInfo->firstname;
	$this->view->email = $userInfo->email_address;
	//$this->view->userlevel = $userInfo->userlevel;
	$_SESSION['userlevel'] = $userInfo->userlevel;
	$this->view->userlevel = $_SESSION['userlevel'];
	
	$user_permission = $user_model->getUserPermissionByType($userInfo->userlevel);
	$this->view->user_permission = $user_permission;		
  }
  
  public function resetpasswdAction()
  {
	Zend_Loader::loadFile('../application/admin/models/User.php');   	
  	$user_model = new User();
	$request = $this->getRequest();
	$username = $request->getParam('username');  
	$reset_password = $user_model->resetPassword($username);
	exit;
  }
  
  public function adminprofileimageAction()
  {
    $request = $this->getRequest();
    $destination_path = "C:/wamp/www/lionparadisev1.0/public/images/";
    $result = 0;
    $target_path = $destination_path . basename( $_FILES['myfile']['name']);
    if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
        echo "DONE";
    }
    
    exit;
  }
  
  public function loadmoduleAction()
  {
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    $request = $this->getRequest();
    $url = $request->getParam('url');         
    
    $mod = explode('/', $url);
    $module = $mod[2];
    
    switch ($module) {        
        case "user" :
            $this->_helper->actionStack('index', 'user');
            break;
            
        case "newsletter" :
            $this->_helper->actionStack('index', 'newsletter');
            break;
            
        case "template" :
            $this->_helper->actionStack('index', 'template');
            break; 
            
        case "hotel" :
            $this->_helper->actionStack('index', 'hotel');
            break;
            
        case "tour" :
            $this->_helper->actionStack('index', 'tour');
            break;
            
        case "aboutcountry" :
            $this->_helper->actionStack('index', 'aboutcountry');
            break;     
            
        case "roomcats" :
            $this->_helper->actionStack('index', 'roomcats');
            break;
            
        case "rates" :
            $this->_helper->actionStack('index', 'rates');
            break;                         
            
        case "translate" :
            $this->_helper->actionStack('index', 'translate');
            break;    
                
        case "permission" :
            $this->_helper->actionStack('index', 'permission');
            break;
            
		case "hotdeals" :
            $this->_helper->actionStack('index', 'hotdeals');
            break;            
            
        case "configuration" :
            $this->_helper->actionStack('index', 'configuration');
            break;
        case "excursion" :
            $this->_helper->actionStack('index', 'excursion');
            break;
       case "campaign" :
            $this->_helper->actionStack('index', 'campaign');
            break;
        case "specialtext" :
            $this->_helper->actionStack('index', 'specialtext');
            break;
        default :
            $this->_helper->actionStack('index', 'index');
            break;    
    }    
  }

  /*public function loadpageAction()
  {
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    $request = $this->getRequest();
    $url = $request->getParam('url');         
    
    $mod = explode('/', $url);
    $module = $mod[2];    
    $action = $mod[3];
    
    switch ($action) {        
        case "adduser" :
            $this->_helper->actionStack('adduser', 'user');
            break;
            
        case "edituser" :
            $this->_helper->actionStack('edituser', 'user');
            break;
            
        case "managetranslate" :
            $this->_helper->actionStack('managetranslate', 'translate');
            break; 
            
        case "edittranslate" :
            $this->_helper->actionStack('edittranslate', 'translate');
            break;
            
        case "roomcats" :
            $this->_helper->actionStack('index', 'roomcats');
            break;
            
        case "rates" :
            $this->_helper->actionStack('index', 'rates');
            break;                         
            
        case "translate" :
            $this->_helper->actionStack('index', 'translate');
            break;    
                
        case "permission" :
            $this->_helper->actionStack('index', 'permission');
            break;
            
        default :
            $this->_helper->actionStack('index', 'index');
            break;    
    }    
  }*/ 
}
?>
